﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace TomAndJerryMidnightSnack
{
    class AnimationJerry
    {
        // The image representing the collection of images used for animation
        Texture2D spriteStrip;

        // The scale used to display the sprite strip
        float scale;

        // The time since we last updated the frame
        int elapsedTime;

        // The time we display a frame until the next one
        int frameTime;

        // The number of frames that the animation contains
        int frameCount;

        // The index of the current frame we are displaying
        public int CurrentFrame;
        public int CurrentRow;
        int[] arrayLengthRow = { 9,  6, 8, 6 };

        // The color of the frame we will be displaying
        Color color;

        // The area of the image strip we want to display
        Rectangle sourceRect = new Rectangle();

        // The area where we want to display the image strip in the game

        // Width of a given frame
        public int FrameWidth;

        // Height of a given frame
        public int FrameHeight;

        // The state of the Animation
        public bool Active;
        public bool Jump;
        public bool Fly;
        public int[] JumpArray = { 0, 8, 16, 24, 16, 8, 0};
        int index;

        // Determines if the animation will keep playing or deactivate after one run
        public bool Looping;

        // Width of a given frame
        public Vector2 Position;

        public void Initialize(Texture2D texture, Vector2 position, int frameWidth, int frameHeight, int frameCount,
                int frametime, Color color, float scale, bool looping)
        {
            // Keep a local copy of the values passed in
            this.color = color;
            this.FrameWidth = frameWidth;
            this.FrameHeight = frameHeight;
            this.frameCount = frameCount;
            this.frameTime = frametime;
            this.scale = scale;

            Looping = looping;
            Position = position;
            spriteStrip = texture;

            // Set the time to zero
            elapsedTime = 0;
            CurrentFrame = 0;

            // Set the Animation to active by default
            Active = true;
            Jump = false;
            Fly = false;
        }
        public void Update(GameTime gameTime)
        {
            // Do not update the game if we are not active
            if (Active == false)
                return;

            // Update the elapsed time
            elapsedTime += (int)gameTime.ElapsedGameTime.TotalMilliseconds;

            // If the elapsed time is larger than the frame time
            // we need to switch frames
            if (Jump && JumpArray.Length>index)
                Position.X += JumpArray[index];

            if (elapsedTime > frameTime)
            {
                if (Jump)
                {
                    if (index < JumpArray.Length)
                    {
                        Position.X += JumpArray[index++];
                    }
                    else
                    {
                        index = 0;
                        Jump = false;
                       // Debug.WriteLine("dung nhay");
                    }
                }
                // Move to the next frame
                CurrentFrame++;

                // If the currentFrame is equal to frameCount reset currentFrame to zero
                if (CurrentFrame == arrayLengthRow[CurrentRow])
                {
                    CurrentFrame = 0;
                    // If we are not looping deactivate the animation
                    if (Looping == false)
                        Active = false;
                }

                // Reset the elapsed time to zero
                elapsedTime = 0;
            }

            // Grab the correct frame in the image strip by multiplying the currentFrame index by the frame width
            sourceRect = new Rectangle(CurrentFrame * FrameWidth, CurrentRow * FrameHeight, FrameWidth, FrameHeight);
           // Debug.WriteLine("row " + CurrentRow);
            // Grab the correct frame in the image strip by multiplying the currentFrame index by the frame width
            /*   destinationRect = new Rectangle((int)Position.X - (int)(FrameWidth * scale) / 2,
               (int)Position.Y - (int)(FrameHeight * scale) / 2,
               (int)(FrameWidth * scale),
               (int)(FrameHeight * scale));*/
        }

        // Draw the Animation Strip
        public void Draw(SpriteBatch spriteBatch)
        {
            // Only draw the animation when we are active
            if (Active)
            {
                //  spriteBatch.Draw(spriteStrip, destinationRect, sourceRect, color);
                Vector2 origin = new Vector2(FrameWidth*frameCount, FrameHeight);
                // spriteBatch.Draw(spriteStrip, new Vector2(200, 100), sourceRect, Color.White, 0.0f, origin, 1.0f, SpriteEffects.None, 0.0f);
                spriteBatch.Draw(spriteStrip, Position, sourceRect, Color.White, MathHelper.PiOver2, origin, scale, SpriteEffects.None, 0f);
              //  Debug.WriteLine("nhuthuy");
               //Debug.WriteLine(Position.X+ "===" + Position.Y );//+ "===" + destinationRect.Width+"===="+ destinationRect.Height + "==="+sourceRect.Width +"==="+ sourceRect.Height+"+++"+FrameHeight + "==="+ FrameWidth);
            }
        }
    }
}
